﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Windows;
using System.Xml.Linq;

using Perseus.Xml;

using Hetymine.Executioner.Commands;

namespace Hetymine.Executioner.Plugins.Url {
    // Multiinput currently disabled until testing param entry
    public class UrlCommand : ICommand, IMultiInput {
        private static UrlCommandConfig _Configurator;
        
        public UrlCommand() {
            this.Name = string.Empty;
            this.Url = string.Empty;
            this.Copy = false;

            this.Inputs = new List<string>(new string[] { "string" });
            this.EnabledInputs = new List<string>(new string[] { "string" });
        }

        public string Url { get; set; }
        public bool Copy { get; set; }

        public string Name { get; set; }
        public bool Execute(string parameters) {
            try {
                string url = this.Url.Replace(
                    "{URL}",
                    HttpUtility.UrlEncode(parameters)
                );
                if (url.IndexOf("://", StringComparison.Ordinal) < 0) {
                    url = "http://" + url;
                }
                System.Diagnostics.Process.Start(url);
                if (this.Copy) {
                    Clipboard.SetText(url);
                }

                return true;
            }
            catch { }

            return false;
        }

        public void Load(XElement element) {
            this.Name = element.Element("Name").ValueOrEmpty();
            this.Url = element.Element("Url").ValueOrEmpty();
            this.Copy = element.Attribute("Copy").ValueOrFalse();

            this.EnabledInputs = Helper.Inputs(element.Attribute("Inputs"), this.Inputs.ToArray());
        }
        public XElement Save() {
            if (this.IsEmpty) {
                throw new Exception("Cannot save empty url command.");
            }
            
            XElement xe = new XElement("Plugin.Url");
            if (this.Copy) {
                xe.Add(new XAttribute("Copy", "true"));
            }
            xe.Add(new XElement("Name", this.Name));
            xe.Add(new XElement("Url", this.Url));

            return xe;
        }
        public bool IsEmpty {
            get {
                return (this.Name.Trim() == string.Empty || this.Url.Trim() == string.Empty);
            }
        }

        public ICommandConfigurator<ICommand> Configurator {
            get {
                if (UrlCommand._Configurator == null) {
                    UrlCommand._Configurator = new UrlCommandConfig();
                }
                UrlCommand._Configurator.InitializeConfig(this);
                return UrlCommand._Configurator;
            }
        }

        #region IMultiInput Members
        public List<string> Inputs { get; protected set; }
        public List<string> EnabledInputs { get; protected set; }
        public void EnableInput(string input) {
            if (this.Inputs.Contains(input)) {
                if (!this.EnabledInputs.Contains(input)) {
                    this.EnabledInputs.Add(input);
                }
            }
        }
        public void DisableInput(string input) {
            this.EnabledInputs.Remove(input);
        }
        #endregion

        public override string ToString() {
            if (this.Name == string.Empty) {
                return string.Empty;
            }

            string s = this.Name;

            if (this.EnabledInputs.Contains("audio")) {
                if (this.EnabledInputs.Contains("string")) {
                    s += " (string, audio)";
                }
                else {
                    s += " (audio)";
                }
            }

            return s;
        }

        public object Clone() {
            UrlCommand urlc = (UrlCommand)this.MemberwiseClone();

            urlc.Inputs = new List<string>();
            foreach (string s in this.Inputs) {
                urlc.Inputs.Add(s);
            }

            return urlc;
        }
    }
}
